package day02;

import util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TestTransaction {

    public static void main(String[] args) {


        Connection con=null;
        PreparedStatement ps=null;

        try{
            con= JdbcUtil.getConnection();

            con.setAutoCommit(false);

            String sql=new StringBuffer()
                    .append("insert into t_user ")
                    .append("    (name) ")
                    .append("values ")
                    .append("    (?) ")
                    .toString();

            ps=con.prepareStatement(sql);




            ps.setString(1,"aaaa3");
            ps.executeUpdate();

            // int i=1/0;

            ps.setString(1,"bbbb3");
            ps.executeUpdate();



            con.commit();



        }catch (Exception e){
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }finally{
            JdbcUtil.close(con,ps,null);
        }


    }


}
